package project_package;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class CustomerRecords
{
	private static CustomerRecords instance = null;
	HashMap<Integer, Customer> customers = new HashMap<Integer, Customer>();
	int customerIndex = 0;
	
	public static CustomerRecords getInstance()
	{
		if(instance == null)
		{
			instance = new CustomerRecords();
			
		}
		return instance;
	}
	
	public boolean createCustomer(String name, String phoneNo, String address, long civilNum, double discount, boolean isBusiness)
	{
		boolean notFound = true;

		Set<Map.Entry<Integer, Customer>> st = customers.entrySet();

		for(Map.Entry<Integer, Customer> customer : st)
		{  
			if (customer.getValue().getName().equals(name))
				notFound = false;
		} 

		if (notFound) 
		{
			Customer newCustomer = new Customer(customerIndex, name, phoneNo, address, civilNum, discount, isBusiness);
			this.customers.put(customerIndex, newCustomer);
			customerIndex++;
		}
		return notFound;
	}
	
	public boolean updateCustomer(int id, String name, String phoneNo, String address, long civilNum, long datestamp, double discount, boolean isBusiness)
	{
		boolean updated = false;
		
		Set<Map.Entry<Integer, Customer>> st = customers.entrySet();
		 
    	for(Map.Entry<Integer, Customer> customer : st)
    	{  
    	      if (customer.getKey().equals(id))
    	      {
				customer.getValue().setName(name);
				customer.getValue().setPhoneNo(phoneNo);
				customer.getValue().setAddress(address);
				customer.getValue().setCivilNum(civilNum);
				customer.getValue().setDatestamp(datestamp);
				customer.getValue().setDiscount(discount);
				customer.getValue().setIsBusiness(isBusiness);
				updated = true;
			}
		}
    	
    	return updated;
	}
	
	 public boolean deleteCustomer(String name)
	 {
		 Customer customer = findCustomer(name);
		 if (customer != null) 
		 {
			 int id = customer.getID();
			 customers.remove(id);
			 return true;
		 } 
		 else {
			 return false;
		 }
	 }
	
	 public Customer findCustomer(String name)
	 {
		 Set<Map.Entry<Integer, Customer>> st = customers.entrySet();

		 for(Map.Entry<Integer, Customer> customer : st)
		 {  
			 if (customer.getValue().getName().equals(name))
			 {
				 return customer.getValue();
			 }
		 }
		 return null;
	 }
	
	 public void printCustomers()
	 {
		 if (!customers.isEmpty()) 
		 {
			 Customer tempCustomer;

			 Set<Map.Entry<Integer, Customer>> st = customers.entrySet();

			 for(Map.Entry<Integer, Customer> customer : st)
			 {  
				 tempCustomer = customer.getValue();
				 System.out.println("Customer ID: " + tempCustomer.getID() + 
						 " - Customer Name: " + tempCustomer.getName() + 
						 " - Customer Type: " + (tempCustomer.getIsBusiness() ? "Business" : "Normal") + 
						 " - Customer Since: " + tempCustomer.getDate());
			 }
		 }
	 }

	 public HashMap<Integer, Customer> getCustomers()
	 {
		 return customers;
	 }
	
	 public int getCustomersNumber()
	 {
		 int customerNumber = customers.size();
		 return customerNumber;
	 }
}